<?php 
namespace app\admin\controller;
use think\Db;
use think\Controller;
use think\Request;
class Star extends Controller
{
    public function index()
    {
        $user = db('user')->select();
        $star = db('star')->select();
        $this->assign('star',$star);
        $this->assign('user',$user);
        return view('admin-star');
    }

    public function add()
    {
        return view('add');
    }
    public function ad()
    {
        if(Request::instance()->isPost()){
            if(input('selec') == ''){
                return '<script>alert("尚未选择用户");window.location="index"</script>';
                exit;
            }

            if(db('star')->where('uid',input('selec'))->find()){
                return '<script>alert("该用户已经是明星");window.location="index"</script>';
                exit;
            }
            $user = db('user')->where('id',input('selec'))->find();
            $rec = db('star')->where('id',input('id'))->update(['name'=>$user['nickname'],'uid'=>$user['id']]);
            if($rec){
                return '<script>alert("修改成功");window.location="index"</script>';
            }else{
                return '<script>alert("修改失败");window.location="index"</script>';
            }
            
        }
        

    }

    public function del()
    {
        if(Request::instance()->isPost()){
        $sql = db('star')->where('id',input('id'))->update(['name'=>'','uid'=>'']);
        if($sql){
            return 'ok';
        }else{
            return 'no';;
        }
    }
    }
}